package com.cosw.util;

import android.content.Context;
import android.content.SharedPreferences;

public class MySharedPreferences {
	public static String PREFERENCE_NAME = "RJDATA";

	private static SharedPreferences shared;

	public static SharedPreferences getShared(Context context) {
		// MODE_WORLD_WRITEABLE����SharedPreferences�ɱ�����Ӧ�÷���
		if (shared == null) {
			shared = context.getSharedPreferences(context.getPackageName()
					+ "_preferences", Context.MODE_PRIVATE);
		}
		return shared;
	}

	public static SharedPreferences.Editor getEditor(Context context) {
		SharedPreferences.Editor editor = getShared(context).edit();
		return editor;
	}

	public static void saveValue(Context context, String key, String value) {
		SharedPreferences.Editor editor = getEditor(context);
		editor.putString(key, value);
		editor.commit();
	}

	public static void saveValue(Context context, String key, int value) {
		SharedPreferences.Editor editor = getEditor(context);
		editor.putInt(key, value);
		editor.commit();
	}
	
	public static void saveValue(Context context, String key, float value) {
		SharedPreferences.Editor editor = getEditor(context);
		editor.putFloat(key, value);
		editor.commit();
	}

	public static void saveValue(Context context, String key, boolean value) {
		SharedPreferences.Editor editor = getEditor(context);
		editor.putBoolean(key, value);
		editor.commit();
	}

	public static final String findValue(Context context, String key, String defaultStr) {
		SharedPreferences shared = getShared(context);
		return shared.getString(key, defaultStr);
	}

	public static final int findValue(Context context, String key, int defValue) {
		SharedPreferences shared = getShared(context);
		return shared.getInt(key, defValue);
	}
	
	public static final float findValue(Context context, String key, float defValue) {
		SharedPreferences shared = getShared(context);
		return shared.getFloat(key, defValue);
	}

	public static final boolean findValue(Context context, String key, boolean defValue) {
		SharedPreferences shared = getShared(context);
		return shared.getBoolean(key, defValue);
	}
	
	/**
     * get float preferences
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     * this name that is not a float
     */
    public static float getFloat(Context context, String key, float defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getFloat(key, defaultValue);
    }
	
	/**
     * put float preferences
     * 
     * @param context
     * @param key The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent storage.
     */
    public static boolean putFloat(Context context, String key, float value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }

    /**
     * get float preferences
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
     * name that is not a float
     * @see #getFloat(Context, String, float)
     */
    public static float getFloat(Context context, String key) {
        return getFloat(context, key, -1);
    }

}
